#include "stdafx.h"
#include "resource.h"
#include "Frame.h"
#include "App.h"
//#include "Editor.h"

#include "ButtonWin32.h"

#include "DefaultContent.h"

#include "test.h"

#include "util/Util.h"



HWND Frame::Create(int x, int y, int width, int height){
	static bool registed = false;
	static const wchar_t* clsName = L"myFrame";
	if(!registed){
		WNDCLASSEXW wndcls = {0};
		wndcls.cbSize= sizeof(WNDCLASSEX);
		wndcls.style = CS_HREDRAW | CS_VREDRAW;
		wndcls.lpfnWndProc = &WinMsg::StaticWndProc;
		wndcls.cbClsExtra = 0;
		wndcls.cbWndExtra = sizeof(void*);
		wndcls.hInstance = GetModuleHandle(0);
		wndcls.hbrBackground = (HBRUSH)(COLOR_WINDOW +1);
		wndcls.hCursor = LoadCursor(NULL, IDC_ARROW);
		wndcls.hIcon = LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(IDI_YEDITOR));
		wndcls.hIconSm = LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(IDI_SMALL));
		wndcls.lpszClassName = clsName;
		wndcls.lpszMenuName =0;
		registed= RegisterClassEx(&wndcls) != 0;
	}
	if(!registed){
		return 0;
	}
	return ::CreateWindowEx(0, clsName, L"", 
		WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 
		x, y, width, height, 0, 0, GetModuleHandle(0), (WinBase*)this);

}

Frame::Frame(){
}

//Editor* Frame::GetEditor(){
//	return &editor_;
//}
void Frame::CreateMainMenu(){
	HWND hWnd = GetWnd();
		
	HMENU oldMenu = GetMenu(hWnd);
	if(oldMenu){
		DestroyMenu(oldMenu);
	}
	
	MenuBuilder mFile;
	mFile.CreateAsSub();
	mFile.AppendItem(L"file.new",		L"New\tCtrl+N");
	mFile.AppendItem(L"file.open",		L"Open\tCtrl+O");
	mFile.AppendItem(L"file.save",		L"Save\tCtrl+S");
	mFile.AppendItem(L"file.saveas",	L"Save As...");
	mFile.AppendItem(L"file.saveall",	L"Save All\tCtrl+Shift+S");
	mFile.AppendSeprator();
	mFile.AppendItem(L"file.exit",		L"Exit\tAlt+X");

	MenuBuilder mEdit;
	mEdit.CreateAsSub();
	mEdit.AppendItem(L"edit.copy",		L"Copy");
	mEdit.AppendItem(L"edit.cut",		L"Cut");
	mEdit.AppendItem(L"edit.paste",		L"Paste");
	mEdit.AppendSeprator();
	mEdit.AppendItem(L"edit.redo",		L"Redo");
	mEdit.AppendItem(L"edit.undo",		L"Undo");



	MenuBuilder mWindow;
	mWindow.CreateAsSub();
	mWindow.AppendItem(L"buffers.close",			L"Close");
	mWindow.AppendItem(L"buffers.closeother",		L"Close Other");
	mWindow.AppendItem(L"buffers.closeall",			L"Close All");
	mWindow.AppendSeprator();

	MenuBuilder mHelp;
	mHelp.CreateAsSub();
	mHelp.AppendItem(L"help.about",		L"About");

	MenuBuilder mTest;
	mTest.CreateAsSub();
	mTest.AppendItem(L"test.one",		L"One");
	mTest.AppendItem(L"test.two",		L"Two");
	mTest.AppendItem(L"test.three",		L"Three");
	mTest.AppendItem(L"test.four",		L"Four");
	mTest.AppendItem(L"test.five",		L"Five");

	MenuBuilder menuBar;
	menuBar.Create();
	menuBar.AppendSubMenu(L"File",		mFile);
	menuBar.AppendSubMenu(L"Edit",		mEdit);
	menuBar.AppendSubMenu(L"Window",	mWindow);
	menuBar.AppendSubMenu(L"Help",		mHelp);
	menuBar.AppendSubMenu(L"Test",		mTest);
	SetMenu(hWnd, menuBar);
	DrawMenuBar(hWnd);
}
void Frame::InitMenu(){
	CreateMainMenu();
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"file.new"), &Frame::OnFileNew, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"file.open"), &Frame::OnFileOpen, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"file.save"), &Frame::OnFileSave, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"file.saveas"), &Frame::OnFileSaveAs, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"file.saveall"), &Frame::OnFileSaveAll, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"file.exit"), &Frame::OnFileExit, this);

	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"edit.copy"), &Frame::OnEditCopy, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"edit.cut"), &Frame::OnEditCut, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"edit.paste"), &Frame::OnEditPaste, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"edit.undo"), &Frame::OnEditUndo, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"edit.redo"), &Frame::OnEditRedo, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"help.about"), &Frame::OnHelpAbout, this);


	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"test.one"), &Frame::OnTestOne, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"test.two"), &Frame::OnTestTwo, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"test.three"), &Frame::OnTestThree, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"test.four"), &Frame::OnTestFour, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"test.five"), &Frame::OnTestFive, this);
}
void Frame::LayoutChildren(){
	
}
void Frame::CreateChildren(){
	
}

void Frame::OnBindMsg(){
	BIND_MSG_MEM(GetWnd(), WM_CREATE, &Frame::OnCreate, this );
	BIND_MSG_MEM(GetWnd(), WM_PAINT, &Frame::OnPaint, this );
	BIND_MSG_MEM(GetWnd(), WM_DESTROY, &Frame::OnDestroy, this );
	BIND_MSG_MEM(GetWnd(), WM_SIZE, &Frame::OnSize, this );
	BIND_MSG_MEM(GetWnd(), WM_SETFOCUS, &Frame::OnFocus, this );
	BIND_MSG_MEM(GetWnd(), WM_COMMAND, &Frame::OnCommand, this );

}
MSGPROC(Frame::OnCreate){
	InitMenu();
	CreateChildren();
	LayoutChildren();

	ButtonWin32* button = 0;
	button = new ButtonWin32();
	button->SetText(L"Hello");
	button->Create(GetWnd());


	HWND w = button->GetUIHand();
	w = button->GetWnd();;
	BIND_MSG_MEM(w, WM_LBUTTONDOWN, &Frame::OnButtonClick, this);
	return true;
}
MSGPROC(Frame::OnButtonClick){
	//::MessageBox(0, L"Hello", L"Test", MB_OK);
	MSG_HANDLED;
	return 0;
}
MSGPROC(Frame::OnFocus){

	MSG_HANDLED;
	return 0;
}

MSGPROC(Frame::OnFileNew){
	//GetEditor()->NewFile();
	MSG_HANDLED;
	return 0;
}
MSGPROC(Frame::OnFileOpen){
	/*vector<wstring> files = util::OpenFileDialog(GetWnd(), true);
	foreach(wstring& f, files){
		GetEditor()->OpenFile(f);
	}*/
	MSG_HANDLED;
	return 0;
}
MSGPROC(Frame::OnFileSave){
	//GetEditor()->SaveFile();
	MSG_HANDLED;
	return 0;
}
MSGPROC(Frame::OnFileSaveAs){
	/*wstring filename = util::SaveFileDialog(GetWnd());
	if(!filename.empty()){
		GetEditor()->SaveAsFile(filename);
	}*/
	MSG_HANDLED;
	return 0;
}
MSGPROC(Frame::OnFileSaveAll){
	//GetEditor()->SaveAllFile();
	MSG_HANDLED;
	return 0;
}
MSGPROC(Frame::OnFileExit){
	DestroyWindow();
	MSG_HANDLED;
	return 0;
}

MSGPROC(Frame::OnEditCopy){
	util::dbg(L"EditCopy");
	MSG_HANDLED;
	return 0;
}
MSGPROC(Frame::OnEditCut){
	util::dbg(L"EditCut");
	MSG_HANDLED;
	return 0;
}
MSGPROC(Frame::OnEditPaste){
	util::dbg(L"EditPaste");
	MSG_HANDLED;
	return 0;
}
MSGPROC(Frame::OnEditUndo){
	util::dbg(L"EditUndo");
	MSG_HANDLED;
	return 0;
}
MSGPROC(Frame::OnEditRedo){
	util::dbg(L"EditRedo");
	MSG_HANDLED;
	return 0;
}

MSGPROC(Frame::OnHelpAbout){
	MessageBox(0, L"about", 0, 0);
	return 0;
}




MSGPROC(Frame::OnSize){
	LayoutChildren();
	MSG_HANDLED;
	return 0;
}
MSGPROC(Frame::OnPaint){
	PAINTSTRUCT ps = {0};
	HDC hdc = BeginPaint(hWnd, &ps);

	EndPaint(hWnd, &ps);
	MSG_HANDLED;
	return 0;
}
MSGPROC(Frame::OnDestroy){
	App::Exit();
	MSG_HANDLED;
	return 0;
}
MSGPROC(Frame::OnCommand){
	//SendMessage(GetEditor()->GetWnd(), uMsg, wParam, lParam);
	return 0;
}

MSGPROC(Frame::OnTestOne){
	testContent();
	MSG_HANDLED;
	return 0;
}
MSGPROC(Frame::OnTestTwo){
	Assert("3<4", 3<4);
	Assert("5>9", 5>9);
	return 0;
}
MSGPROC(Frame::OnTestThree){
	Log<<sizeof(HWND)<<endl;
	return 0;
}
MSGPROC(Frame::OnTestFour){
	return 0;
}
MSGPROC(Frame::OnTestFive){
	return 0;
}